﻿/****************************** Module Header ******************************\
 * Module Name:  MessageBox.cs
 * Module Version:  1.0.0 (2012-8-10)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used for MessageBox.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Web.UI;

namespace UCL.Web
{
    /// <summary>
    /// 消息窗口管理类
    /// <para>Version:2011-12-26</para>
    /// <para>提供页面弹出窗口的方法</para>
    /// </summary>
    public class MessageBox
    {
        /*
         * 消息的几种类型：
         * Asterisk	:	该消息框包含一个符号，该符号是由一个圆圈及其中的小写字母 i 组成的
         * Hand	:	该消息框包含一个符号，该符号是由一个红色背景的圆圈及其中的白色 X 组成的
         * Exclamation	:	该消息框包含一个符号，该符号是由一个黄色背景的三角形及其中的一个感叹号组成的
         * Question	:	该消息框包含一个符号，该符号是由一个圆圈和其中的一个问号组成的
         */

        /// <summary>
        /// Js框架
        /// <para>Jeasyui=Jquery Easy UI</para>
        /// <para>JqueryUI=Jquery-UI</para>
        /// </summary>
        public static string Jsframework = "Jeasyui";

        /// <summary>
        /// JeasyUI消息构造器
        /// </summary>
        /// <param name="message"></param>
        /// <param name="title"></param>
        /// <param name="isModal"></param>
        /// <param name="buttons"></param>
        /// <returns></returns>
        private static string JeasyUIMessager(string message, string title, bool isModal, string buttons)
        {
            return "$.messager.alert('{0}','{1}','{2}',{3});".Fmt(title, message, "", buttons);
        }

        /// <summary>
        /// JqueryUI消息构造器
        /// </summary>
        /// <param name="message"></param>
        /// <param name="title"></param>
        /// <param name="isModal"></param>
        /// <param name="buttons"></param>
        /// <returns></returns>
        private static string JqueryUIMessager(string message, string title, bool isModal, string buttons)
        {
            return "$('<div>" + message + "</div>').dialog({title:'" + title + "',modal:" + isModal.ToString().ToLower() + buttons + "})";
        }

        /// <summary>
        /// 弹出消息窗口
        /// </summary>
        /// <param name="page"></param>
        /// <param name="message"></param>
        /// <param name="title"></param>
        /// <param name="action"></param>
        /// <param name="isModal"></param>
        /// <param name="buttons"></param>
        public static void Show(Page page, string message, string title, string action, bool isModal, string buttons)
        {
            switch (action)
            {
                case "goback":
                case "history.back":
                case "history.back()":
                case "parent.history.back()":
                    action = "parent.history.back();";
                    break;
                case "":
                    break;
                default:
                    action = "parent.location.href='{0}';".Fmt(action);
                    break;
            }
            string strScript = "";
            switch (Jsframework)
            {
                case "Jeasyui":
                    strScript = JeasyUIMessager(message, title, isModal, "function(){" + action + "}");
                    break;
                default:
                    string strOnClose = (action.Length == 0) ? "$(this).dialog('close');" : action;
                    buttons = (buttons.Length == 0) ? ",buttons:{'确定':function(){" + strOnClose + "}}" : ",buttons:{" + buttons + ",'确定':function(){" + strOnClose + "}}";
                    strScript = JqueryUIMessager(message, title, isModal, buttons);
                    break;
            }
            JScriptManager.CreateJqueryDocumentReadyScript(page, strScript);
        }

        /// <summary>
        /// 弹出消息窗口
        /// </summary>
        /// <param name="page"></param>
        /// <param name="message"></param>
        /// <param name="title"></param>
        /// <param name="action"></param>
        public static void Show(Page page, string message, string title, string action)
        {
            Show(page, message, title, action, true, "");
        }

        /// <summary>
        /// 弹出消息窗口
        /// </summary>
        /// <param name="page"></param>
        /// <param name="message"></param>
        /// <param name="action"></param>
        public static void Show(Page page, string message, string action)
        {
            Show(page, message, "提示消息", action, true, "");
        }

        public static void Show(string message, string url = "", string title = "提示")
        {
            _Response.Write("<script type=\"text/javascript\" id=\"easyui\">(function(){if($('script[id=easyui]').length>1)return;var links=['/DMS/Skin/Js/JqPlus/Jeasyui/last/themes/default/easyui.css','/DMS/Skin/Js/JqPlus/Jeasyui/last/themes/icon.css'];var scripts=['/DMS/Skin/Js/JqPlus/Jeasyui/last/jquery.easyui.min.js'];var scriptsrc=\"/DMS/Skin/Js/JqPlus/Jeasyui/last/jquery.easyui.min.js\";var $head=$(document.documentElement.firstChild);var $scripts=$head.children('script');var hasSource=false;$scripts.each(function(){var $this=$(this);if($this.attr(\"src\")==scriptsrc){hasSource=true;return;}});if(!hasSource){if(links){$.each(links,function(i,n){$head.append('<link type=\"text/css\" rel=\"Stylesheet\" href=\"'+n+'\" />');});}if(scripts){$.each(scripts,function(i,n){$head.append('<script type=\"text/javascript\" src=\"'+n+'\"><\\/script>');});}}})();</script>");
            _Response.Write("<script>$.messager.alert('{0}', '{1}',''{2});</script>".Fmt(title, message, url.Length > 0 ? ",function(){{location.href='{0}';}}".Fmt(url) : ""));

        }

        /// <summary>
        /// 页面消息提示
        /// </summary>
        /// <param name="page"></param>
        /// <param name="message"></param>
        /// <param name="url"></param>
        public static void FlyOut(Page page, string message, string url)
        {
            JScriptManager.RegisterStartupScript(page, "page.FlyOut('{0}','',{1})".Fmt(message, (url.Length > 0 ? "location.href='" + url + "'" : "")));
        }

        /// <summary>
        /// 弹出一个IFrame窗口
        /// </summary>
        /// <param name="page"></param>
        /// <param name="src"></param>
        /// <param name="config"></param>
        public static void ShowIframe(Page page, string src, string config)
        {
            if (config.Length > 0) config = "," + config;
            string strScript = "$(\"<iframe src='" + src + "' frameborder='0' scrolling='no'></iframe>\").dialog({title:'窗口',modal:true" + config + "});";
            JScriptManager.CreateJqueryDocumentReadyScript(page, strScript);
        }

        /// <summary>
        /// 弹出js警告框(如果地址不为空则自动跳转到指定地址)
        /// <para>例如：MessageBox.Alert("操作成功","?success");</para>
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="location"></param>
        public static void Alert(string msg, string location = "", bool endResponse = true)
        {
            string s = "alert('{0}');".Fmt(msg);
            s = "{0}{1}".Fmt(s, location.Length > 0 ? "location.href='{0}';".Fmt(location) : "");
            _Response.WriteJScript(s,endResponse);
        }
    }
}
